package cn.topcode.unicorn.wxsdk.wxpay;

import cn.topcode.unicorn.utils.XmlUtil;
import cn.topcode.unicorn.utils.http.HttpResp;
import cn.topcode.unicorn.utils.http.HttpUtil;
import cn.topcode.unicorn.wxsdk.wxpay.dto.paytouser.GetTransferInfo;
import cn.topcode.unicorn.wxsdk.wxpay.dto.paytouser.GetTransferInfoResult;
import cn.topcode.unicorn.wxsdk.wxpay.dto.paytouser.PayToUser;
import cn.topcode.unicorn.wxsdk.wxpay.dto.paytouser.PayToUserResult;

/**
 * Created by Unicorn on 2018/2/25.
 */
public class WxPayToUserInvoker {

    /**
     * 向用户付款接口地址
     */
    public static final String TRANSFERS = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

    /**
     * 查询企业付款信息
     */
    public static final String GET_TRANSFER_INFO = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo";

    /**
     * 企业向用户付款
     * @param payToUser
     * @return
     */
    public PayToUserResult payToUser(PayToUser payToUser) {
        String xml = XmlUtil.toXml(payToUser);
        HttpResp httpResp = HttpUtil.postText(TRANSFERS, xml);
        if(httpResp.getStatusCode() != 200) {
            return null;
        }
        return XmlUtil.parse(httpResp.getBody(), PayToUserResult.class);
    }

    /**
     * 查询企业付款信息
     * @param getTransferInfo
     * @return
     */
    public GetTransferInfoResult getTransferInfo(GetTransferInfo getTransferInfo) {
        String xml = XmlUtil.toXml(getTransferInfo);
        HttpResp httpResp = HttpUtil.postText(GET_TRANSFER_INFO, xml);
        if(httpResp.getStatusCode() != 200) {
            return null;
        }
        return XmlUtil.parse(httpResp.getBody(), GetTransferInfoResult.class);
    }
}
